package cn.player.act;

import cn.player.R;
import cn.player.view.FlakeView;
import cntv.player.core.util.LogUtil;
import cntv.player.media.player.IMediaPlayer;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class FlakeViewActivity extends BaseActivity {
	
	private LinearLayout mContainer;
	private RelativeLayout mRlRoot;
	private FlakeView mFlakeView;
	
	
	protected int startX;
	protected int startY;
	protected int moveX;
	protected int moveY; 
	/**
	 * 是否滑动开始
	 */
	private boolean isSlideStart;
	/** * 左右滑动时的阻力系数(较小)*/
	private static final float RADIO_H = 2.3f; 
	/*** 上下滑动时的阻力系数(较大) */
	private static final float RADIO_V = 123f; 
	/*** 横向移动距离*/
	protected int diffX;
	/*** 竖向移动距离 */
	protected int diffY;
	/** * 横向最终移动距离，除以左右滑动阻力系数后的值*/
	private int distanceH = 0;
	/*** 竖向移动距离，除以上下滑动阻力系数后的值*/
	private int distanceV = 0;
	/**
	 * 当滑动大于mDistanceToSlide的值时，代表滑动开始，否则视为点击
	 */
	private int mDistanceToSlide = 12;
	
	/*** 滑动方向，左、右、上、下 */
	private int slideState = IMediaPlayer.SLIDE_STATE_NONE;
	/*** 滑动方向，横向、竖向*/
	private int currentSlideState = IMediaPlayer.SLIDE_STATE_NONE;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.flake_view);
		
		findViews();
		setListeners();

	}

	private void findViews() {
		// TODO Auto-generated method stub
		mContainer = (LinearLayout) findViewById(R.id.container); 
		mRlRoot = (RelativeLayout) findViewById(R.id.rl_flake_root); 
		
		mFlakeView = new FlakeView(this);
		mContainer.addView(mFlakeView);
	}
	
	private void setListeners() {
		// TODO Auto-generated method stub
		
		OnTouchListener mOnTouchListener = new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent ev) {
				// TODO Auto-generated method stub
				switch (ev.getAction()) {
	    		case MotionEvent.ACTION_DOWN:
//	    			LogUtil.i("zl", "MotionEvent.ACTION_DOWN");
	    			startX = (int)ev.getX();
	    			startY = (int) ev.getY();
	    			
	    			break;
	    		case MotionEvent.ACTION_MOVE:
//	    			LogUtil.i("zl", "MotionEvent.ACTION_MOVE");
	    			moveX = (int) ev.getX();
	    			moveY = (int) ev.getY(); 
	    			
	    			// 当前触摸的点到上一个触摸点的位移
	    			diffX = moveX - startX;
	    			diffY = moveY - startY;
	    			
	    			if(Math.abs(diffY) < Math.abs(diffX) && Math.abs(diffX)>=mDistanceToSlide){//代表左右滑动
	    				if(currentSlideState != IMediaPlayer.SLIDE_STATE_VERTICAL){//防止横向滑动的时候未松手又纵向滑动
	    					currentSlideState = IMediaPlayer.SLIDE_STATE_HORIZONRAL;
	    					slideState = diffX>0 ? IMediaPlayer.SLIDE_STATE_RIGHT : IMediaPlayer.SLIDE_STATE_LEFT;
	    					diffX /= RADIO_H;
	        				if(!isSlideStart){
	        					isSlideStart = true;
//	        					mOnSlideListener.onSlideStart(slideState);
	        				}
	        				if(diffX != distanceH){
	        					distanceH = diffX;
//	        					mOnSlideListener.onSliding(slideState, distanceH);
	        				}
	    				}
	    				
	    			}else if(Math.abs(diffY) > Math.abs(diffX) && Math.abs(diffY)>=mDistanceToSlide){//代表上下滑动
	    				if(currentSlideState != IMediaPlayer.SLIDE_STATE_HORIZONRAL){//防止纵向滑动的时候未松手又横向滑动
	    					currentSlideState = IMediaPlayer.SLIDE_STATE_VERTICAL;
	    					slideState = diffY>0 ? IMediaPlayer.SLIDE_STATE_DOWN : IMediaPlayer.SLIDE_STATE_UP;
	    					diffY /= RADIO_V;
	    					
	        				if(!isSlideStart){
	        					isSlideStart = true;
//	        					mOnSlideListener.onSlideStart(slideState);
	        				}
	        				if(diffY != distanceV){
	        					distanceV = diffY;
//	        					mOnSlideListener.onSliding(slideState, distanceV);
	        					setFlakesNum(distanceV);
	        				}
	    				}
	    			}
	    			break;
	    		case MotionEvent.ACTION_UP:
//	    			LogUtil.i("zl", "MotionEvent.ACTION_UP");
	    			moveX = (int) ev.getX();
	    			moveY = (int) ev.getY(); 
	    			
	    			diffX = 0;
	    			diffY = 0;
	    			isSlideStart = false;
					if (currentSlideState == IMediaPlayer.SLIDE_STATE_HORIZONRAL) {
//						mOnSlideListener.onSlideEnd(slideState, distanceH);
						distanceH = 0;
						slideState = IMediaPlayer.SLIDE_STATE_NONE;
						currentSlideState = IMediaPlayer.SLIDE_STATE_NONE;
					} else if (currentSlideState == IMediaPlayer.SLIDE_STATE_VERTICAL) {
//						mOnSlideListener.onSlideEnd(slideState, distanceV);
						distanceV = 0;
						slideState = IMediaPlayer.SLIDE_STATE_NONE;
						currentSlideState = IMediaPlayer.SLIDE_STATE_NONE;
					} 
	    			break;
	    		case MotionEvent.ACTION_CANCEL:
	    			LogUtil.i("zl", "MotionEvent.ACTION_CANCEL");
	    			slideState = IMediaPlayer.SLIDE_STATE_NONE;
	    			currentSlideState = IMediaPlayer.SLIDE_STATE_NONE;
//	    			mOnSlideListener.onSlideEnd(slideState, 0);
					distanceV = 0;
					distanceH = 0;
	    			
	    			break;
	    		default:
	    			break;
	    		}
	        	return true;
			}
			
		};
		
		mRlRoot.setOnTouchListener(mOnTouchListener);
		
	}
	
	private void setFlakesNum(int distanceV){
		if(distanceV > 0){
//			mFlakeView.addFlakes(mFlakeView.getNumFlakes());
			mFlakeView.addFlakes(4);
		}else{
//			mFlakeView.subtractFlakes(mFlakeView.getNumFlakes() / 2);
			mFlakeView.subtractFlakes(mFlakeView.getNumFlakes() > 4 ? 4 : mFlakeView.getNumFlakes() / 2);
		}
	}
	
	
	@Override
	protected void onPause() {
		super.onPause();
		mFlakeView.pause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		mFlakeView.resume();
	}
}
